from turbogears.feed        import FeedController
from fasttrack.utils        import getRecentChanges, textilize
from fasttrack.model        import Project  
import cherrypy             as http

    
class FastTrackFeed(FeedController):
    
    def get_feed_data(self):
        entrys = []
        all_projects = Project.select("parent_project_id is NULL order by upper(name)")
        projects = [project for project in all_projects if not project.archived]
        print self._timeframe()
        recent_proj = getRecentChanges(projects, self._timeframe())
        
        for proj in recent_proj:
            entry = {}
            if len(proj.recent_issues) > 0:
                for issue in proj.recent_issues:    
                    entry["updated"] = issue.creation_date
                    entry["title"] = "Issue:%s" % issue.project.name
                    entry["published"] = issue.creation_date
                    entry["author"] = issue.creator.displayName
                    entry["link"] = http.config.get('fasttrack.recentchangesurl')
                    entry["summary"] = textilize(issue.issue[:30])
                    entrys.append(entry)
                    entry = {}
                    
            if len(proj.recent_risks) > 0:
                 for risk in proj.recent_risks:
                    entry["updated"] = risk.creation_date
                    entry["title"] = "Risk:%s" % risk.project.name
                    entry["published"] = risk.creation_date
                    entry["author"] = risk.creator.displayName
                    entry["link"] = http.config.get('fasttrack.recentchangesurl')
                    entry["summary"] = textilize(risk.description[:30])
                    entrys.append(entry)
                    entry = {}
                    
            if len(proj.recent_notes) > 0:
                for note in proj.recent_notes:
                    entry["updated"] = note.creation_date
                    entry["title"] = "Risk:%s" % note.project.name
                    entry["published"] = note.creation_date
                    entry["author"] = note.creator.displayName
                    entry["link"] = http.config.get('fasttrack.recentchangesurl')
                    entry["summary"] = textilize(note.note[:30])
                    
                    entrys.append(entry)
                    entry = {}

            if len(proj.recent_questions) > 0:
                for question in proj.recent_questions:
                    entry["updated"] = question.creation_date
                    entry["title"] = "Question:%s" % question.project.name
                    entry["published"] = question.creation_date
                    entry["author"] = question.creator.displayName
                    entry["link"] = http.config.get('fasttrack.recentchangesurl')
                    entry["summary"] = textilize(question.question[:30])
                    entrys.append(entry)
                    entry = {}
                    if len(question.answers) > 0:
                        for answer in question.answers:
                            entry["updated"] = answer.creation_date
                            entry["title"] = "Question:Answer %s:%s" % (question.question[:30], question.project.name)
                            entry["published"] = answer.creation_date
                            entry["author"] = answer.creator.displayName
                            entry["link"] = http.config.get('fasttrack.recentchangesurl')
                            entry["summary"] = textilize(answer.answer[:30])
                            entrys.append(entry)
                            entry = {}

        
        return dict(
                    title="fasttrack recent changes",
                    link="http://10.10.150.90/dashboard",
                    author={ "name" : "Tim Jones", "email" : "tjones@optio.com" },
                    id="http://10.10.150.90/dashboard",
                    subtitle="recent changes feed",
                    entrys=entrys
               )
               

    def _timeframe(self):
            if http.request.simpleCookie.has_key('time_frame_id'):
                return int(http.request.simpleCookie['time_frame_id'].value)
            return 24
